// HSS.Forms.Docking.AutoHideStrip.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       AutoHideStrip.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HSS.Forms.Docking
{
	internal class AutoHideStrip : AutoHideStripBase
	{
		#region AutoHideTab
		private class AutoHideTab : Tab
		{
			internal AutoHideTab(IDockContent content)
				: base(content)
			{
			}

			private int _tabX = 0;
			public int TabX
			{
				get { return _tabX; }
				set { _tabX = value; }
			}

			private int _tabWidth = 0;
			public int TabWidth
			{
				get { return _tabWidth; }
				set { _tabWidth = value; }
			}

		}
		#endregion

		private const int _ImageHeight = 16;
		private const int _ImageWidth = 16;

		private const int _ImageGapTop = 2;
		private const int _ImageGapLeft = 2;
		private const int _ImageGapRight = 2;
		private const int _ImageGapBottom = 2;

		private const int _TextGapLeft = 2;
		private const int _TextGapRight = 2;

		private const int _TabGapTop = 2;
		private const int _TabGapLeft = 2;

		private const int _TabGapBetween = 8;

		#region Customizable Properties
		private static Font TextFont
		{
			get { return SystemInformation.MenuFont; }
		}

		private static StringFormat _stringFormatTabHorizontal;
		private StringFormat StringFormatTabHorizontal
		{
			get
			{
				if (_stringFormatTabHorizontal == null)
				{
					_stringFormatTabHorizontal = new StringFormat();
					_stringFormatTabHorizontal.Alignment = StringAlignment.Near;
					_stringFormatTabHorizontal.LineAlignment = StringAlignment.Center;
					_stringFormatTabHorizontal.FormatFlags = StringFormatFlags.NoWrap;
				}

				if (RightToLeft == RightToLeft.Yes)
					_stringFormatTabHorizontal.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
				else
					_stringFormatTabHorizontal.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;

				return _stringFormatTabHorizontal;
			}
		}

		private static StringFormat _stringFormatTabVertical;
		private StringFormat StringFormatTabVertical
		{
			get
			{
				if (_stringFormatTabVertical == null)
				{
					_stringFormatTabVertical = new StringFormat();
					_stringFormatTabVertical.Alignment = StringAlignment.Near;
					_stringFormatTabVertical.LineAlignment = StringAlignment.Center;
					_stringFormatTabVertical.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.DirectionVertical;
				}
				if (RightToLeft == RightToLeft.Yes)
					_stringFormatTabVertical.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
				else
					_stringFormatTabVertical.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;

				return _stringFormatTabVertical;
			}
		}

		private static int ImageHeight
		{
			get { return _ImageHeight; }
		}

		private static int ImageWidth
		{
			get { return _ImageWidth; }
		}

		private static int ImageGapTop
		{
			get { return _ImageGapTop; }
		}

		private static int ImageGapLeft
		{
			get { return _ImageGapLeft; }
		}

		private static int ImageGapRight
		{
			get { return _ImageGapRight; }
		}

		private static int ImageGapBottom
		{
			get { return _ImageGapBottom; }
		}

		private static int TextGapLeft
		{
			get { return _TextGapLeft; }
		}

		private static int TextGapRight
		{
			get { return _TextGapRight; }
		}

		private static int TabGapTop
		{
			get { return _TabGapTop; }
		}

		private static int TabGapLeft
		{
			get { return _TabGapLeft; }
		}

		private static int TabGapBetween
		{
			get { return _TabGapBetween; }
		}

		private static Brush BrushTabBackground(Rectangle rect, bool vert, bool left)
		{
			if (!vert)
			{
				if (left)
					return new LinearGradientBrush(rect, Color.White, Color.LightGray, LinearGradientMode.Vertical);
				else
					return new LinearGradientBrush(rect, ControlPaint.LightLight(SystemColors.ControlDark), Color.White, LinearGradientMode.Vertical);
			}
			else
				return new LinearGradientBrush(rect, Color.White, ControlPaint.LightLight(SystemColors.ControlDark), LinearGradientMode.Vertical);
		}

		private static Pen PenTabBorder
		{
			get { return SystemPens.GrayText; }
		}

		private static Brush BrushTabText
		{
			get { return SystemBrushes.ControlText; }
		}
		#endregion

		private static Matrix _matrixIdentity = new Matrix();
		private static Matrix MatrixIdentity
		{
			get { return _matrixIdentity; }
		}

		private static DockState[] _dockStates;
		private static DockState[] DockStates
		{
			get
			{
				if (_dockStates == null)
				{
					_dockStates = new DockState[4];
					_dockStates[0] = DockState.DockLeftAutoHide;
					_dockStates[1] = DockState.DockRightAutoHide;
					_dockStates[2] = DockState.DockTopAutoHide;
					_dockStates[3] = DockState.DockBottomAutoHide;
				}
				return _dockStates;
			}
		}

		private static GraphicsPath _graphicsPath;
		internal static GraphicsPath GraphicsPath
		{
			get
			{
				if (_graphicsPath == null)
					_graphicsPath = new GraphicsPath();

				return _graphicsPath;
			}
		}

		public AutoHideStrip(DockPanel panel)
			: base(panel)
		{
			SetStyle(ControlStyles.ResizeRedraw |
				ControlStyles.UserPaint |
				ControlStyles.AllPaintingInWmPaint |
				ControlStyles.OptimizedDoubleBuffer, true);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			if (this.BackColor != DockPanel.EnvironmentBackColor)
				this.BackColor = DockPanel.EnvironmentBackColor;
			Graphics g = e.Graphics;
			DrawTabStrip(g);
		}

		protected override void OnLayout(LayoutEventArgs levent)
		{
			CalculateTabs();
			base.OnLayout(levent);
		}

		private void DrawTabStrip(Graphics g)
		{
			DrawTabStrip(g, DockState.DockTopAutoHide, true);
			DrawTabStrip(g, DockState.DockBottomAutoHide, true);
			DrawTabStrip(g, DockState.DockLeftAutoHide, false);
			DrawTabStrip(g, DockState.DockRightAutoHide, false);
		}

		private void DrawTabStrip(Graphics g, DockState dockState, bool vert)
		{
			Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);

			if (rectTabStrip.IsEmpty)
				return;

			Matrix matrixIdentity = g.Transform;
			if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
			{
				Matrix matrixRotated = new Matrix();
				matrixRotated.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
					(float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
				g.Transform = matrixRotated;
			}

			foreach (Pane pane in GetPanes(dockState))
			{
				foreach (AutoHideTab tab in pane.AutoHideTabs)
					DrawTab(g, tab, vert);
			}
			g.Transform = matrixIdentity;
		}

		private void CalculateTabs()
		{
			CalculateTabs(DockState.DockTopAutoHide);
			CalculateTabs(DockState.DockBottomAutoHide);
			CalculateTabs(DockState.DockLeftAutoHide);
			CalculateTabs(DockState.DockRightAutoHide);
		}

		private void CalculateTabs(DockState dockState)
		{
			Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);

			int imageHeight = rectTabStrip.Height - ImageGapTop - ImageGapBottom;
			int imageWidth = ImageWidth;
			if (imageHeight > ImageHeight)
				imageWidth = ImageWidth * (imageHeight / ImageHeight);

			int x = TabGapLeft + rectTabStrip.X;
			foreach (Pane pane in GetPanes(dockState))
			{
				foreach (AutoHideTab tab in pane.AutoHideTabs)
				{
					int width = imageWidth + ImageGapLeft + ImageGapRight +
						TextRenderer.MeasureText(tab.Content.DockHandler.TabText, TextFont).Width +
						TextGapLeft + TextGapRight;
					tab.TabX = x;
					tab.TabWidth = width;
					x += width;
				}

				x += TabGapBetween;
			}
		}

		private Rectangle RtlTransform(Rectangle rect, DockState dockState)
		{
			Rectangle rectTransformed;
			if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
				rectTransformed = rect;
			else
				rectTransformed = DrawHelper.RtlTransform(this, rect);

			return rectTransformed;
		}

		private GraphicsPath GetTabOutline(AutoHideTab tab, bool transformed, bool rtlTransform)
		{
			DockState dockState = tab.Content.DockHandler.DockState;
			Rectangle rectTab = GetTabRectangle(tab, transformed);
			if (rtlTransform)
				rectTab = RtlTransform(rectTab, dockState);

			GraphicsPath graphicsPath = GraphicsPath;
			if (graphicsPath == null)
				graphicsPath = new GraphicsPath();
			else
				graphicsPath.Reset();
			graphicsPath.AddRectangle(rectTab);

			return GraphicsPath;
		}

		private void DrawTab(Graphics g, AutoHideTab tab, bool vert)
		{
			Rectangle rectTabOrigin = GetTabRectangle(tab);
			if (rectTabOrigin.IsEmpty)
				return;

			DockState dockState = tab.Content.DockHandler.DockState;
			IDockContent content = tab.Content;

			bool left = tab.Content.DockHandler.Pane.DockState == DockState.DockLeftAutoHide;
			bool hover = false;
			if (tab.Content == HitTest(PointToClient(Control.MousePosition)))
				hover = true;

			// Draw Background and Outline
			GraphicsPath path = GetTabOutline(tab, false, true);
			if (hover)
			{
				Color fillColor1 = Color.GhostWhite;
				Color fillColor2 = Color.SkyBlue;
				if (!vert)
				{
					if (left)
					{
						fillColor1 = Color.GhostWhite;
						fillColor2 = ControlPaint.Light(Color.LightSkyBlue);
					}
					else
					{
						fillColor1 = ControlPaint.Light(Color.LightSkyBlue);
						fillColor2 = Color.GhostWhite;
					}
				}
				using (LinearGradientBrush brush = new LinearGradientBrush(path.GetBounds(), fillColor1, fillColor2, LinearGradientMode.Vertical))
					g.FillPath(brush, path);
			}
			else
			{
				g.FillPath(BrushTabBackground(rectTabOrigin, vert, left), path);
			}
			g.DrawPath(PenTabBorder, path);

			// Set no rotate for drawing icon and text
			Matrix matrixRotate = g.Transform;
			g.Transform = MatrixIdentity;

			// Draw the icon
			Rectangle rectImage = rectTabOrigin;
			rectImage.X += ImageGapLeft;
			rectImage.Y += ImageGapTop;
			int imageHeight = rectTabOrigin.Height - ImageGapTop - ImageGapBottom;
			int imageWidth = ImageWidth;
			if (imageHeight > ImageHeight)
				imageWidth = ImageWidth * (imageHeight / ImageHeight);
			rectImage.Height = imageHeight;
			rectImage.Width = imageWidth;
			rectImage = GetTransformedRectangle(dockState, rectImage);
			g.DrawIcon(((Form)content).Icon, RtlTransform(rectImage, dockState));

			// Draw the text
			Rectangle rectText = rectTabOrigin;
			rectText.X += ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
			rectText.Width -= ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
			rectText = RtlTransform(GetTransformedRectangle(dockState, rectText), dockState);
			if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
				g.DrawString(content.DockHandler.TabText, TextFont, BrushTabText, rectText, StringFormatTabVertical);
			else
				g.DrawString(content.DockHandler.TabText, TextFont, BrushTabText, rectText, StringFormatTabHorizontal);

			// Set rotate back
			g.Transform = matrixRotate;
		}

		private Rectangle GetLogicalTabStripRectangle(DockState dockState)
		{
			return GetLogicalTabStripRectangle(dockState, false);
		}

		private Rectangle GetLogicalTabStripRectangle(DockState dockState, bool transformed)
		{
			if (!DockHelper.IsDockStateAutoHide(dockState))
				return Rectangle.Empty;

			int leftPanes = GetPanes(DockState.DockLeftAutoHide).Count;
			int rightPanes = GetPanes(DockState.DockRightAutoHide).Count;
			int topPanes = GetPanes(DockState.DockTopAutoHide).Count;
			int bottomPanes = GetPanes(DockState.DockBottomAutoHide).Count;

			int x, y, width, height;

			height = MeasureHeight();
			if (dockState == DockState.DockLeftAutoHide && leftPanes > 0)
			{
				x = 0;
				y = (topPanes == 0) ? 0 : height;
				width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 : height);
			}
			else if (dockState == DockState.DockRightAutoHide && rightPanes > 0)
			{
				x = Width - height;
				if (leftPanes != 0 && x < height)
					x = height;
				y = (topPanes == 0) ? 0 : height;
				width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 : height);
			}
			else if (dockState == DockState.DockTopAutoHide && topPanes > 0)
			{
				x = leftPanes == 0 ? 0 : height;
				y = 0;
				width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
			}
			else if (dockState == DockState.DockBottomAutoHide && bottomPanes > 0)
			{
				x = leftPanes == 0 ? 0 : height;
				y = Height - height;
				if (topPanes != 0 && y < height)
					y = height;
				width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
			}
			else
				return Rectangle.Empty;

			if (!transformed)
				return new Rectangle(x, y, width, height);
			else
				return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
		}

		private Rectangle GetTabRectangle(AutoHideTab tab)
		{
			return GetTabRectangle(tab, false);
		}

		private Rectangle GetTabRectangle(AutoHideTab tab, bool transformed)
		{
			DockState dockState = tab.Content.DockHandler.DockState;
			Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);

			if (rectTabStrip.IsEmpty)
				return Rectangle.Empty;

			int x = tab.TabX;
			int y = rectTabStrip.Y +
				(dockState == DockState.DockTopAutoHide || dockState == DockState.DockRightAutoHide ?
				0 : TabGapTop);
			int width = tab.TabWidth;
			int height = rectTabStrip.Height - TabGapTop;

			if (!transformed)
				return new Rectangle(x, y, width, height);
			else
				return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
		}

		private Rectangle GetTransformedRectangle(DockState dockState, Rectangle rect)
		{
			if (dockState != DockState.DockLeftAutoHide && dockState != DockState.DockRightAutoHide)
				return rect;

			PointF[] pts = new PointF[1];
			// the center of the rectangle
			pts[0].X = (float)rect.X + (float)rect.Width / 2;
			pts[0].Y = (float)rect.Y + (float)rect.Height / 2;
			Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
			Matrix matrix = new Matrix();
			matrix.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
				(float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
			matrix.TransformPoints(pts);

			return new Rectangle((int)(pts[0].X - (float)rect.Height / 2 + .5F),
				(int)(pts[0].Y - (float)rect.Width / 2 + .5F),
				rect.Height, rect.Width);
		}

		protected override IDockContent HitTest(Point ptMouse)
		{
			foreach (DockState state in DockStates)
			{
				Rectangle rectTabStrip = GetLogicalTabStripRectangle(state, true);
				if (!rectTabStrip.Contains(ptMouse))
					continue;

				foreach (Pane pane in GetPanes(state))
				{
					DockState dockState = pane.DockPane.DockState;
					foreach (AutoHideTab tab in pane.AutoHideTabs)
					{
						GraphicsPath path = GetTabOutline(tab, true, true);
						if (path.IsVisible(ptMouse))
							return tab.Content;
					}
				}
			}

			return null;
		}

		protected bool HitTest2(Point ptMouse)
		{
			foreach (DockState state in DockStates)
			{
				Rectangle rectTabStrip = GetLogicalTabStripRectangle(state, true);
				if (!rectTabStrip.Contains(ptMouse))
					continue;

				foreach (Pane pane in GetPanes(state))
				{
					DockState dockState = pane.DockPane.DockState;
					foreach (AutoHideTab tab in pane.AutoHideTabs)
					{
						GraphicsPath path = GetTabOutline(tab, true, true);
						if (path.IsVisible(ptMouse))
							return true;
					}
				}
			}

			return false;
		}

		protected internal override int MeasureHeight()
		{
			return Math.Max(ImageGapBottom +
				ImageGapTop + ImageHeight,
				TextFont.Height) + TabGapTop;
		}

		protected override void OnRefreshChanges()
		{
			CalculateTabs();
			Invalidate();
		}

		protected override AutoHideStripBase.Tab CreateTab(IDockContent content)
		{
			return new AutoHideTab(content);
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if (HitTest2(PointToClient(Control.MousePosition)))
				Invalidate();
		}

		protected override void OnMouseLeave(EventArgs e)
		{
			base.OnMouseLeave(e);
			Invalidate();
		}
	}
}